/**
 * 
 */
package com.gosophia.conferenceManagement.common.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.ConferenceConfiguration;
import com.gosophia.conferenceManagement.common.dto.ConferenceFileInfosDTO;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobDataDTO;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobHandleDTO;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.ConferenceJobData;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.service.ConferenceHandleService;
import com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService;
import com.gosophia.conferenceManagement.scheme.dto.SchemeJobDataDTO;
import com.gosophia.conferenceManagement.scheme.entity.SchemeJobData;
import com.gosophia.conferenceManagement.starting.dto.ConferenceApplicationDTO;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.starting.entity.ConferenceApplicationJobData;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingJobData;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

/**
 * 功能：会议流转webservice 实现
 * 
 * @author zhangdongsheng
 * @since 2011-3-23 Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("conferenceHandleWebService")
public class ConferenceHandleWebServiceImpl implements
		ConferenceHandleWebService {

	private final Logger logger = Logger.getLogger(this.getClass());

	private final Mapper dozer = new DozerBeanMapper();

	@Autowired
	@Qualifier("conferenceHandleService")
	private ConferenceHandleService conferenceHandleService;
	@Autowired
	private TaskManagementService taskManagementService;
	@Autowired
	private UserManagementService userManagementService;

	/*
	 * 从业务进入方案起草（直接发起会议）
	 */
	@Override
	public SchemeJobDataDTO startingConferenceProcessing(
			SchemeJobDataDTO schemeJobDataDTO) throws BusinessException {
		logger.debug("方法:startingConferenceProcessing，参数信息：" + schemeJobDataDTO.toString());

		SchemeJobData data = dozer.map(schemeJobDataDTO, SchemeJobData.class);

		if(schemeJobDataDTO.getImportantLevelDTO()!=null){
			DataValue importLevel = dozer.map(schemeJobDataDTO.getImportantLevelDTO(), DataValue.class);
			data.setImportantLevel(importLevel);// 重要度
		}
		if(schemeJobDataDTO.getConferenceCreatorDTO()!=null){
			UserInfo creator = dozer.map(schemeJobDataDTO.getConferenceCreatorDTO(), UserInfo.class);
			data.setConferenceCreator(creator);// 会议大任务发起人
		}
		if(schemeJobDataDTO.getOnlineDocumentDTO()!=null){
		    data.setOnlineDocument(dozer.map(schemeJobDataDTO.getOnlineDocumentDTO(), Document.class));
		}

		Mission backResult = conferenceHandleService
				.startingConferenceProcessing(data);
		//用于显示发起成功的提示信息
		schemeJobDataDTO.setMissionCode(backResult.getMissionCode());// 返回missionCode

		return schemeJobDataDTO;
	}

	/*
	 * 推荐使用的 conference任务处理方法
	 * 
	 * @see
	 * com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService
	 * #handleConferenceJob
	 * (com.gosophia.conferenceManagement.common.ws.ConferenceJobHandleDTO)
	 */
	@Override
	public void handleConferenceJob(ConferenceJobHandleDTO dto)
			throws BusinessException {
		Assert.notNull(dto, "处理操作信息不能为空");
		ConferenceJobData jobData = convertTo(dto, null);
		conferenceHandleService.HandleConferenceTurning(dto.getJobId(), dto
				.getOperatorId(), jobData);
	}

	/*
	 * 推荐使用的conference 任务处理方法，附带文件信息
	 * 
	 * @see
	 * com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService
	 * #handleConferenceJob
	 * (com.gosophia.conferenceManagement.common.ws.ConferenceJobHandleDTO,
	 * com.gosophia.conferenceManagement.common.ws.ConferenceFileInfosDTO)
	 */
	@Override
	public void handleConferenceJob(ConferenceJobHandleDTO dto,
			ConferenceFileInfosDTO files) throws BusinessException {
		Assert.notNull(dto, "处理操作信息不能为空");
		Assert.notNull(files, "处理的文件信息类不能为空");
		ConferenceJobData jobData = convertTo(dto, files);
		conferenceHandleService.HandleConferenceTurning(dto.getJobId(), dto
				.getOperatorId(), jobData);
	}

	private ConferenceJobData convertTo(ConferenceJobHandleDTO dto,
			ConferenceFileInfosDTO files) {
		ConferenceJobData jobData = new ConferenceJobData();
		// jobData.setJob(dto.getJobId());//设定job
		jobData.setTurningAction(dto.getHandleAction());// 设定处理动作
		jobData.setOpinion(dto.getOpinion());// 设定处理意见
		jobData.setNextApprovalId(dto.getNextOperatorId());

		if (files != null && files.getOnlineDocument() != null)
			jobData.setOnlineDocument(dozer.map(files.getOnlineDocument(),
					Document.class));

		if (files != null && files.getAttachments() != null
				&& !files.getAttachments().isEmpty()) {
			List<DocumentDTO> docs = files.getAttachments();
			List<Document> attachments = new ArrayList<Document>();
			for (DocumentDTO documentDTO : docs) {
				attachments.add(dozer.map(documentDTO, Document.class));
			}
			jobData.setAttachments(attachments);
		}

		return jobData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService
	 * #HandleConferenceTurning(java.lang.Long, java.lang.Long,
	 * com.gosophia.conferenceManagement.common.dto.ConferenceJobDataDTO)
	 */
	@Override
	public void HandleConferenceTurning(Long jobId, Long operatorId,
			ConferenceJobDataDTO conferenceJobDataDTO) throws BusinessException {

		// TODO Auto-generated method stub
		logger.debug("webservice HandleConferenceTurning start");
		ConferenceJobData jobData = dozer.map(conferenceJobDataDTO,
				ConferenceJobData.class);
		conferenceHandleService.HandleConferenceTurning(jobId, operatorId,
				jobData);
		logger.debug("webservice HandleConferenceTurning succ");
	}

	private ConferenceJobData convertJobData(
			ConferenceJobDataDTO conferenceJobDataDTO) {
		return null;
	}

	private ConferenceApplicationJobData convertConferenceApplicationJobData(
			ConferenceStartingDTO dto) {

		ConferenceApplicationJobData jobData = dozer.map(dto,
				ConferenceApplicationJobData.class);

		if (dto.getNextHandlerId() != null) {

		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gosophia.conferenceManagement.common.ws.ConferenceHandleWebService
	 * #handleConferenceAppJob
	 * (com.gosophia.conferenceManagement.starting.dto.ConferenceApplicationDTO,
	 * com.gosophia.conferenceManagement.common.dto.ConferenceJobHandleDTO)
	 */
	@Override
	public void handleConferenceAppJob(
			ConferenceApplicationDTO conferenceApplicationDTO,
			ConferenceJobHandleDTO handleInfo) throws BusinessException {
		// 处理会议申请审批修改
		logger.debug("webservice handleConferenceAppJob start");
		ConferenceApplicationJobData jobData = dozer.map(
				conferenceApplicationDTO, ConferenceApplicationJobData.class);
		jobData.setTurningAction(handleInfo.getHandleAction());
		jobData.setNextApprovalId(handleInfo.getNextOperatorId());
		jobData.setOpinion(handleInfo.getOpinion());
		// 构造undertaker
		if (conferenceApplicationDTO.getConfernceUnderTakerId() != null) {
			UserInfo conferenceUnderTaker = userManagementService
					.findUserInfoByUserInfoId(conferenceApplicationDTO
							.getConfernceUnderTakerId());
			jobData.setConfernceUnderTaker(conferenceUnderTaker);
		}
		
		
		Job job = taskManagementService.findJobDetailByJobId(handleInfo
				.getJobId());
		jobData.setJob(job);

		conferenceHandleService.HandleConferenceTurning(handleInfo.getJobId(),
				handleInfo.getOperatorId(), jobData);
		logger.debug("webservice handleConferenceAppJob succ");

	}

}
